//
//  PKLoginServices.swift
//  Panku
//
//  Created by mac on 2017/2/9.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit
import ReactiveCocoa
import ReactiveSwift
import Result

private let USER_ID = "userId"
private let TOKEN = "token"

class PKLoginServices: NSObject {
    
    //登录
    class func login(accessId:String,accessType:String,passwd:String) -> SignalProducer<Any,NSError>{
        
        let url = PKUrl.USERS_LOGIN_URL
        
        let parameters:[String : Any] = ["accessId":accessId,
                                         "accessType":accessType,
                                         "passwd":passwd]
        let networkUtilsFactory = PKNetworkUtilsHelper.factory()
        let networkUtils = networkUtilsFactory()
        return networkUtils.request(url: url, method: .post, parameters: parameters, headers: nil)
    }
    
    //注册
    class func register(accessId:String,accessType:String,passwd:String) -> SignalProducer<Any,NSError> {
        
        let url = PKUrl.USERS_URL
        
        let parameters:[String : Any] = ["accessId":accessId,
                                         "accessType":accessType,
                                         "passwd":passwd]
        let networkUtilsFactory = PKNetworkUtilsHelper.factory()
        let networkUtils = networkUtilsFactory()
        return networkUtils.request(url: url, method: .post, parameters: parameters, headers: nil)
    }

    
    //修改密码
    class func updatePassword() -> Void{
    
    }
    
    //获取用户信息
    class func getUserProfile() -> Void{
    
    }
    
    //更新用户头像
    class func updateUserAvatar() -> Void{
    
    }
    
    //设置用户id
    class func setUserId(id:Int) -> Void{
        UserDefaults.standard.set(id, forKey: USER_ID)
        
    }
    
    //获取用户id
    class func getUserId() -> Int{
        return UserDefaults.standard.integer(forKey: USER_ID)
        
    }
    
    //设置token
    class func setToken(token:String?) -> Void{
        UserDefaults.standard.set(token, forKey: TOKEN)
        
    }
    
    //退出删除token
    class func deleteToken() -> Void{
        setToken(token: nil)
    }
    
    //获取token
    class func getToken() -> String?{
        return UserDefaults.standard.object(forKey: TOKEN) as? String
    }
    
    //判断是否登录
    class func isLogin() -> Bool{
        if getToken() == nil{
            return false
        }else{
            return true
        }
    }
    
    
}
